package com.auction.mapper;

import com.auction.dto.BidPageQueryDTO;
import com.auction.pojo.Bid;
import com.auction.vo.BidVO;
import com.auction.vo.BidWithTitle;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BidMapper {
        /**
         * 插入竞价记录
         * 
         * @param bid
         */
        @Insert("INSERT INTO bid (auction_id, user_id, bid_price, bid_time, status, create_time, update_time) "
                        +
                        "VALUES (#{auctionId}, #{userId}, #{bidPrice}, #{bidTime}, #{status},  #{createTime}, #{updateTime})")
        void insert(Bid bid);

        /**
         * 查询拍卖品的所有竞价记录
         * 
         * @param auctionId
         * @return
         */
        @Select("SELECT * FROM bid WHERE auction_id = #{auctionId} ORDER BY bid_price DESC")
        List<Bid> selectByAuctionId(@Param("auctionId") Long auctionId);

        /**
         * 查询拍卖品的最高竞价记录
         * 
         * @param auctionId
         * @return
         */
        @Select("SELECT * FROM bid WHERE auction_id = #{auctionId} ORDER BY bid_price DESC LIMIT 1")
        Bid selectHighestBid(@Param("auctionId") Long auctionId);

        /**
         * 更新之前的竞价状态
         * 
         * @param auctionId
         * @param status
         * @return
         */
        @Update("UPDATE bid SET status = #{status} WHERE auction_id = #{auctionId} AND status = 0")
        void updatePreviousBidsStatus(@Param("auctionId") Long auctionId, @Param("status") Integer status);

        /**
         * 查询用户的竞价记录
         * 
         * @param userId
         * @return
         */
        @Select("SELECT * FROM bid WHERE user_id = #{userId} ORDER BY create_time DESC")
        List<Bid> selectByUserId(@Param("userId") Long userId);

        /**
         * 更新竞价状态
         * 
         * @param id
         * @param status
         */
        @Update("UPDATE bid SET status = #{status}, update_time = NOW() WHERE id = #{id}")
        void updateBidStatus(@Param("id") Long id, @Param("status") Integer status);

        /**
         * 更新其他竞价状态
         * 
         * @param auctionId    拍卖品ID
         * @param excludeBidId 排除的竞价ID
         * @param status       状态
         */
        @Update("UPDATE bid SET status = #{status}, update_time = NOW() WHERE auction_id = #{auctionId} AND id != #{excludeBidId}")
        void updateOtherBidsStatus(@Param("auctionId") Long auctionId, @Param("excludeBidId") Long excludeBidId,
                        @Param("status") Integer status);

        /**
         * 分页查询拍卖品的所有竞价记录
         *
         * @return
         */
        @Select("SELECT * FROM bid WHERE user_id = #{userId} ORDER BY create_time DESC")
        List<BidWithTitle> pageByUserIdWithTitle(Long userId);

        /**
         * 分页查询拍卖品所有竞价记录
         *
         * @param bidPageQueryDTO
         * @return
         */
        Page<BidVO> pageQuery(BidPageQueryDTO bidPageQueryDTO);
}